import pymysql


def init_database():
    """初始化数据库和表结构"""
    try:
        # 连接MySQL服务器（不指定数据库）
        connection = pymysql.connect(
            host="127.0.0.1",
            port=3306,
            user="root",
            password="123456",
            charset="utf8mb4"
        )

        cursor = connection.cursor()

        # 创建数据库（如果不存在）
        cursor.execute("CREATE DATABASE IF NOT EXISTS recruitment_analysis")
        cursor.execute("USE recruitment_analysis")

        # 创建数据表
        create_table_sql = """
        CREATE TABLE IF NOT EXISTS bosszp (
            id INT AUTO_INCREMENT PRIMARY KEY,
            job_title VARCHAR(255) NOT NULL,
            job_salary VARCHAR(100),
            job_lable VARCHAR(100),
            job_company VARCHAR(255),
            job_url VARCHAR(500),
            job_company_tag TEXT,
            job_desc TEXT,
            job_acquire TEXT,
            company_city VARCHAR(100),
            job_skill TEXT,
            keyword VARCHAR(100),
            create_time TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
            -- 添加唯一索引来防止重复数据
            UNIQUE KEY unique_job (job_title, job_company, company_city, keyword)
        )
        """

        cursor.execute(create_table_sql)

        print("数据库初始化成功！")
        print("数据库: recruitment_analysis")
        print("数据表: bosszp")
        print("已添加唯一索引防止重复数据")

        cursor.close()
        connection.close()

    except Exception as e:
        print(f"数据库初始化失败: {str(e)}")


if __name__ == "__main__":
    init_database()